package cn.ibizlab.odoo.core.odoo_portal.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iportal_share;
import cn.ibizlab.odoo.core.odoo_portal.domain.Portal_share;
import cn.ibizlab.odoo.core.odoo_portal.filter.Portal_shareSearchContext;
import cn.ibizlab.odoo.core.odoo_portal.service.IPortal_shareService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[门户分享] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Portal_shareServiceImpl implements IPortal_shareService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iportal_shareClientService portal_shareClientService;

    @Override
    public boolean update(Portal_share portal_share) {
        boolean bOk = false ;
        Iportal_share clientModel = this.convert2Model(portal_share,null);
        portal_shareClientService.update(clientModel) ;
        this.convert2Domain(clientModel,portal_share) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Portal_share> portal_shares, int batchSize) {

        //portal_shareClientService.updateBatch(portal_shares,batchSize) ;

    }

    @Override
    public Portal_share get(Integer id) {
        Iportal_share clientModel = portal_shareClientService.createModel();
        clientModel.setId(id);
        portal_shareClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Portal_share get(Portal_share portal_share) {
        Portal_share retportal_share = this.get(portal_share.getId()) ;
    	BeanCopier copier=BeanCopier.create(retportal_share.getClass(), portal_share.getClass(), false);
        copier.copy(retportal_share, portal_share, null);
        return portal_share ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //portal_shareClientService.removeBatch(portal_shares) ;

    }

    @Override
    public boolean create(Portal_share portal_share) {
        boolean bOk = false ;
        Iportal_share clientModel = this.convert2Model(portal_share,null);
        portal_shareClientService.create(clientModel) ;
        this.convert2Domain(clientModel,portal_share);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Portal_share> portal_shares, int batchSize) {

                        //portal_shareClientService.createBatch(portal_shares,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iportal_share clientModel = portal_shareClientService.createModel();
        clientModel.setId(id);
        portal_shareClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Portal_share> searchDefault(Portal_shareSearchContext context) {
        List<Portal_share> list = new ArrayList<Portal_share>();
        Page<cn.ibizlab.odoo.core.client.model.Iportal_share> clientModelList = portal_shareClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iportal_share clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Portal_share>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Portal_share> selectByOdoo_create(Integer id){
        Portal_shareSearchContext context = new Portal_shareSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Portal_share> list = new ArrayList<Portal_share>();
        Page<cn.ibizlab.odoo.core.client.model.Iportal_share> clientModelList = portal_shareClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iportal_share clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Portal_share>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Portal_share> selectByOdoo_write(Integer id){
        Portal_shareSearchContext context = new Portal_shareSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Portal_share> list = new ArrayList<Portal_share>();
        Page<cn.ibizlab.odoo.core.client.model.Iportal_share> clientModelList = portal_shareClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iportal_share clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Portal_share>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iportal_share convert2Model(Portal_share domain , Iportal_share model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = portal_shareClientService.createModel() ;
        }

         if(domain.getShare_linkDirtyFlag())
            model.setShare_link(domain.getShare_link());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getAccess_warningDirtyFlag())
            model.setAccess_warning(domain.getAccess_warning());
         if(domain.getPartner_idsDirtyFlag())
            model.setPartner_ids(domain.getPartner_ids());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getRes_idDirtyFlag())
            model.setRes_id(domain.getRes_id());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getRes_modelDirtyFlag())
            model.setRes_model(domain.getRes_model());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Portal_share convert2Domain( Iportal_share model ,Portal_share domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Portal_share();
        }

        if(model.getShare_linkDirtyFlag())
            domain.setShare_link(model.getShare_link());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getAccess_warningDirtyFlag())
            domain.setAccess_warning(model.getAccess_warning());
        if(model.getPartner_idsDirtyFlag())
            domain.setPartner_ids(model.getPartner_ids());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getRes_idDirtyFlag())
            domain.setRes_id(model.getRes_id());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getRes_modelDirtyFlag())
            domain.setRes_model(model.getRes_model());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
